package com.example.mobile2021606128wje0420;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class PartAdapter extends RecyclerView.Adapter <PartAdapter.ViewHolder>{

    private int[] partId;
    private String[] partName;

    public PartAdapter(int[] partId, String[] partName) {
        this.partId = partId;
        this.partName = partName;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_main_part,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //通过position获取数组元素，赋值给控件imageView和tvView
        holder.imageView.setImageResource(partId[position]);
        holder.textView.setText(partName[position]);

    }

    @Override
    public int getItemCount() {

        return partId.length;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.iv_item_part);
            textView=itemView.findViewById(R.id.tv_item_part);
        }
    }
}
